<?php

namespace app\api\service;

use app\api\model\AddressBook as AddressBookModel;
use app\common\exception\BusinessException;
use think\Db;

/**
 * 地址簿服务
 */
class AddressBook
{
    /**
     * 显示所有地址
     *
     */
    public function showAddressBook($userId)
    {
        $result = [];
        $list = AddressBookModel::all(['user_id' => $userId]);
        if ($list) {
            foreach ($list as $key => $addressBook) {
                $result[] = [
                    'id' => $addressBook->id,
                    'userId' => $addressBook->user_id,
                    'consignee' => $addressBook->consignee,
                    'phone' => $addressBook->phone,
                    'sex' => $addressBook->sex,
                    'provinceCode' => $addressBook->province_code,
                    'provinceName' => $addressBook->province_name,
                    'cityCode' => $addressBook->city_code,
                    'cityName' => $addressBook->city_name,
                    'districtCode' => $addressBook->district_code,
                    'districtName' => $addressBook->district_name,
                    'detail' => $addressBook->detail,
                    'label' => $addressBook->label,
                    'isDefault' => $addressBook->is_default,
                ];
            }
        }
        return $result;
    }

    /**
     * 显示默认地址
     *
     */
    public function showDefaultAddressBook($userId)
    {
        $result = [];
        $addressBook = AddressBookModel::get([
            'user_id' => $userId,
            'is_default' => 1 // 1 是 0 否
        ]);
        if ($addressBook) {
            $result = [
                'cityCode' => $addressBook->city_code,
                'cityName' => $addressBook->city_name,
                'consignee' => $addressBook->consignee,
                'detail' => $addressBook->detail,
                'districtCode' => $addressBook->district_code,
                'districtName' => $addressBook->district_name,
                'id' => $addressBook->id,
                'isDefault' => $addressBook->is_default,
                'label' => $addressBook->label,
                'phone' => $addressBook->phone,
                'provinceCode' => $addressBook->province_code,
                'provinceName' => $addressBook->province_name,
                'sex' => $addressBook->sex,
                'userId' => $addressBook->user_id,

            ];
        }
        return $result;
    }

    /**
     * 添加地址
     *
     */
    public function addAddressBook($data)
    {
        $create = [
            'detail' => $data['detail'],
            'phone' => $data['phone'],
            'sex' => $data['sex'],
            'user_id' => $data['userId'],
        ];

        if (isset($data['cityCode'])) $create['city_code'] = $data['cityCode'];
        if (isset($data['cityName'])) $create['city_name'] = $data['cityName'];
        if (isset($data['consignee'])) $create['consignee'] = $data['consignee'];
        if (isset($data['districtCode'])) $create['district_code'] = $data['districtCode'];
        if (isset($data['districtName'])) $create['district_name'] = $data['districtName'];
        if (isset($data['isDefault'])) $create['is_default'] = $data['isDefault'];
        if (isset($data['label'])) $create['label'] = $data['label'];
        if (isset($data['phone'])) $create['phone'] = $data['phone'];
        if (isset($data['provinceCode'])) $create['province_code'] = $data['provinceCode'];
        if (isset($data['sex'])) $create['sex'] = $data['sex'];

        $res = AddressBookModel::create($create);
        if (!$res) {
            throw new BusinessException('添加地址簿失败');
        }
    }

    /**
     * 修改地址
     *
     */
    public function updateAddressBook($data)
    {
        $update = [
            'detail' => $data['detail'],
            'phone' => $data['phone'],
            'sex' => $data['sex'],
            'user_id' => $data['userId'],
        ];

        if (isset($data['cityCode'])) $update['city_code'] = $data['cityCode'];
        if (isset($data['cityName'])) $update['city_name'] = $data['cityName'];
        if (isset($data['consignee'])) $update['consignee'] = $data['consignee'];
        if (isset($data['districtCode'])) $update['district_code'] = $data['districtCode'];
        if (isset($data['districtName'])) $update['district_name'] = $data['districtName'];
        if (isset($data['isDefault'])) $update['is_default'] = $data['isDefault'];
        if (isset($data['label'])) $update['label'] = $data['label'];
        if (isset($data['phone'])) $update['phone'] = $data['phone'];
        if (isset($data['provinceCode'])) $update['province_code'] = $data['provinceCode'];
        if (isset($data['sex'])) $update['sex'] = $data['sex'];

        $id = $data['id'];
        $addressBook = AddressBookModel::get($id);
        if (!$addressBook) {
            throw new BusinessException('没有找到地址');
        }

        foreach ($update as $key => $val) {
            $addressBook->$key = $val;
        }
        $addressBook->save();
    }

    /**
     * 删除地址
     *
     * @param int $id
     */
    public function deleteAddressBook($id)
    {
        // 根据id查询地址
        $addressBook = AddressBookModel::get($id);
        if (!$addressBook) {
            throw new BusinessException("没有找到地址");
        }
        $addressBook->delete();
    }

    /**
     * 设置默认地址
     *
     * @param int $id
     */
    public function setDefaultAddressBook($id)
    {
        // 根据id查询地址
        $addressBook = AddressBookModel::get($id);
        if (!$addressBook) {
            throw new BusinessException("没有找到地址");
        }
        $defaultAddressBook = AddressBookModel::get(['is_default' => 1]);
        if ($defaultAddressBook) {
            $defaultAddressBook->is_default = 0;
            $defaultAddressBook->save();
        }
        $addressBook->is_default = 1;
        $addressBook->save();
    }

    /**
     * 根据id查询地址
     *
     */
    public function getAddressBookById($id, $userId)
    {
        $addressBook =  AddressBookModel::get($id);
        if (!$addressBook) {
            throw new BusinessException("地址不存在");
        }
        if ($addressBook->user_id != $userId) {
            throw new BusinessException("没有权限获取");
        }
        return [
            'id' => $addressBook->id,
            'phone' => $addressBook->phone,
            'consignee' => $addressBook->consignee,
            'userId' => $addressBook->user_id,
            'cityCode' => $addressBook->city_code,
            'provinceName' => $addressBook->province_name,
            'provinceCode' => $addressBook->province_code,
            'sex' => $addressBook->sex,
            'districtName' => $addressBook->district_name,
            'districtCode' => $addressBook->district_code,
            'cityName' => $addressBook->city_name,
            'isDefault' => $addressBook->is_default,
            'label' => $addressBook->label,
            'detail' => $addressBook->detail,
        ];
    }
}
